package com.ch7_6;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

import java.security.Principal;

/**
 * Created by wangjing on 2018/7/18.
 */
@Controller
public class WsController {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    /**
     * 一对一
     * @param principal
     * @param msg
     */
    @MessageMapping("/chat")
    public void handleChat(Principal principal,String msg){
        if(principal.getName().equals("heqiang")){
            simpMessagingTemplate.convertAndSendToUser("wisely","/queue/notifications",
                    principal.getName()+"-send:"+msg);
        } else{
            simpMessagingTemplate.convertAndSendToUser("heqiang","/queue/notifications",
                    principal.getName()+"-send:"+msg);
        }
    }

    /**
     * 广播
     * @param message
     * @return
     * @throws Exception
     */
    @MessageMapping("/welcome")
    @SendTo("/topic/getResponse")
    public WiselyResponse say(WiselyMessage message) throws Exception{
        Thread.sleep(3000);
        return new WiselyResponse("welcome,"+message.getName()+"!!!!!!!");
    }
}
